/*
 * JMEDesktopUtil.java
 *
 * Created on Jul 9, 2007, 9:53:44 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.util;

import com.jmex.awt.swingui.JMEDesktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author DGronau
 */
public class JMEDesktopUtil {

    public enum H {

        LEFT, CENTER, RIGHT
    }
    {
    }

    public enum V {

        TOP, CENTER, BOTTOM
    }
    {
    }


    public static int width;
    public static int height;

    private JMEDesktopUtil() {
    }

    public static void init(JDesktopPane jDesktopPane) {
        JMEDesktopUtil.width = jDesktopPane.getWidth();
        JMEDesktopUtil.height = jDesktopPane.getHeight();
    }

    /**
     * This method sets the location of a JComponent. The coordinates 0, 0
     * mark the <b>center</b> of the screen.
     * @param component the component
     * @param x the x coordinate relative to the center of the screen
     * @param y the y coordinate relative to the cennter of the screen
     * @param horizontal should the component be left, right or centered relative to the x-coordinate
     * @param vertical should the component be top, bottom or centered relative to the y-coordinate
     */
    public static void pos(JComponent component, int x, int y, H horizontal, V vertical) {
        int left = width / 2 + x;
        if (horizontal == H.CENTER) {
            left -= component.getWidth() / 2;
        }
        if (horizontal == H.RIGHT) {
            left -= component.getWidth();
        }

        int top = height / 2 + y;
        if (vertical == V.CENTER) {
            top -= component.getHeight() / 2;
        }
        if (vertical == V.BOTTOM) {
            top -= component.getHeight();
        }

        component.setLocation(left, top);
    }

    /**
     * Assigns the maximum preferred size from all components as the size
     * of all components.
     *
     * @param comps the components
     */
    public static void maxSize(JComponent... comps) {
        Dimension size = new Dimension(0, 0);
        for (JComponent comp : comps) {
            if (size.getWidth() < comp.getPreferredSize().getWidth()) {
                size = comp.getPreferredSize();
            }
        }
        for (JComponent comp : comps) {
            comp.setSize(size);
        }
    }

    /**
     * Assigns the preferred size as size.
     *
     * @param comps the components
     */
    public static void setSize(JComponent... comps) {
        for (JComponent comp : comps) {
            comp.setSize(comp.getPreferredSize());
        }
    }

    public static void setJLabel(JDesktopPane jDesktop, String text, Point pos, Font font) {
        JLabel label = new JLabel(text);
        label.setFont(font);
        jDesktop.add(label);
        setSize(label);
        pos(label, pos.x, pos.y, H.RIGHT, V.CENTER);
    }

    public static void setJComboBox(JDesktopPane jDesktop, JComboBox box, String[] values, Point pos) {
        jDesktop.add(box);
        if (values != null) {
           for (String value : values) {
               box.addItem(value);
           }
        }
        setSize(box);
        pos(box, pos.x, pos.y, H.LEFT, V.CENTER);
        box.invalidate();
        box.validate();
        box.repaint();
    }
    
     public static void setJComboBox(JDesktopPane jDesktop, JComboBox box, Point pos) {
         setJComboBox(jDesktop, box, null, pos);
    }

    /**
     * Shows a message dialog.
     *
     * @param desktop the current desktop
     * @param title the caption of the dialog
     * @param message the message of the dialog
     * @param messageType the meesageType of the dialog (e.g. JOprionPane.ERROR_MESSAGE)
     */
    public static void showMessageDialog(final JMEDesktop desktop, String title, String message, int messageType) {
        final JInternalFrame modalDialog = new JInternalFrame(title);
        JOptionPane optionPane = new JOptionPane(message, messageType);
        modalDialog.getContentPane().add(optionPane);
        desktop.setModalComponent(modalDialog);
        desktop.getJDesktop().add(modalDialog, 0);
        modalDialog.setVisible(true);
        modalDialog.setSize(modalDialog.getPreferredSize());
        pos(modalDialog, 0, 0, JMEDesktopUtil.H.CENTER, JMEDesktopUtil.V.CENTER);
        desktop.setFocusOwner(optionPane);

        optionPane.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                modalDialog.setVisible(false);
                desktop.setModalComponent(null);
                desktop.getJDesktop().remove(modalDialog);
            }
        });
    }
}
